package com.pignest.hotel.module.global;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.hotel.beans.User;
import com.pignest.hotel.service.UserService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;

@Deprecated
public class UserWindow extends Window {
	private static final long serialVersionUID = 2147239767923279156L;
	private String userId;
	private User user;

	private Textbox nameField;
	private Textbox workNoField;
	private Textbox cardNoField;

	private Textbox dutyField;
	private Listbox sexField;
	private Textbox mobileField;

	private Datebox birthdayField;
	private Textbox mailField;
	private Textbox nativePlaceField;

	private Textbox qqField;
	private Textbox msnField;
	private Textbox workPhoneField;

	private Textbox homeAddressField;
	private Textbox homePhoneField;
	private Textbox homePostField;

	private void init() {

		nameField = (Textbox) getFellow("nameField");
		workNoField = (Textbox) getFellow("workNoField");
		cardNoField = (Textbox) getFellow("cardNoField");

		dutyField = (Textbox) getFellow("dutyField");
		sexField = (Listbox) getFellow("sexField");
		mobileField = (Textbox) getFellow("mobileField");

		birthdayField = (Datebox) getFellow("birthdayField");
		mailField = (Textbox) getFellow("mailField");
		nativePlaceField = (Textbox) getFellow("nativePlaceField");

		qqField = (Textbox) getFellow("qqField");
		msnField = (Textbox) getFellow("msnField");
		workPhoneField = (Textbox) getFellow("workPhoneField");

		homeAddressField = (Textbox) getFellow("homeAddressField");
		homePhoneField = (Textbox) getFellow("homePhoneField");
		homePostField = (Textbox) getFellow("homePostField");
	}

	private void setUser(User user) {
		init();
		System.out.println("name:" + user.getName());
		nameField.setValue(user.getName());
		workNoField.setValue(user.getWorkNumber());
		cardNoField.setValue(user.getCardNumber());

		UIUtils.setSelectedItem(sexField,user.getGender());
		mobileField.setValue(user.getPhone());

	}

	public String save() {

		init();
		String name = nameField.getValue();
		String workNo = workNoField.getValue();
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(workNo)) {
			return "用户名或者账号不能为空!";
		}
		UserService server = Services.getUserService();
		if (StringUtils.isEmpty(userId)) {// 新增
			User user = new User();
			user.setName(name);
//			if (server.exists(workNo)) {
//				return "该账号已存在请选择其它账号!";
//			}
			user.setWorkNumber(workNo);
			user.setCardNumber(cardNoField.getValue());
			server.insert(user);
		} else {
			setUserId(userId);
			if (server.existWorkNoOther(userId,workNo)) {
				return "修改已存在的账号!";
			}
			user.setWorkNumber(workNo);
			user.setCardNumber(cardNoField.getValue());
//			user.setSex(sexField.getSelectedItem().getValue().toString());
			user.setGender((String)UIUtils.getSelectedValue(sexField));
			server.update(user);
		}
		return null;
	}

	public void setUserId(String userId) {

		System.out.println("userId:" + userId);
		try {
			if (userId != null) {
				User user = Services.getUserService().get(User.class, userId);
				if (user != null) {
					this.userId = userId;
					this.user = user;
				}
			}
			setUser(user);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
